$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }


    /**
     * 事件绑定
     */
    function bindEvent(){
        //根据条件查询商品信息
        $('.inner-section #search_btn').click(function() {
            render(true,1,pageSize);
            return false;
        });
        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });  

        //批量删除
        $(".content .right-section .delete-batch").click(function(){
        	var ids = idList.join(',');
            if(ids == ""){
        		$("#myModal .modal-body").html("<p class='text-danger'>您尚未选择要删除的选项，请先选择！</p>");
                $("#myModal").modal('show');
                //定时器，1.5秒后模态框自动关闭
                setTimeout(function(){
                    $("#myModal").modal('hide');
                },1500);
        	}else{
	            myConfirmModal("确定要批量删除吗？",function(){
		            $.ajax({
		                url:"deleteGroup",
		                type:"post",
		                data:{"ids":ids},
		                dataType:"json",
		                beforeSend:function(xhr){
		                    //显示“加载中。。。”
		                    $("#loading").modal('show');
		                },
		                complete:function(){
		                    //隐藏“加载中。。。”
		                    $("#loading").modal('hide');
		                },
		                success:function(json,statusText){
		                    if(json.errorCode == 0){
		                    	if(currentPage != 1 && (total_count - idList.length) % pageSize == 0){
	                                currentPage = currentPage - 1;
	                            }
	                            idList = [];//初始化idList的值
	                            render(true,currentPage,pageSize);
		                    }else{
		                        responseTip(1,json.errorInfo);
		                    }
		                },
		                error:errorResponse
		            });
	            });
        	}
        });
    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {
            good_name : $("#good_name").val(),
            is_success : $("#is_success").val()
        };
        return selectInfo;
    }
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);

    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingGroup',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html+='<tr><th class="th1"><input type="checkbox" class="select-all my-icheckbox"></th><th class="th1">序号</th><th class="th2">商品名称</th><th class="th2">团长昵称</th><th class="th3">开团人数</th><th class="th4">剩余人数</th><th class="th7">拼团状态</th><th class="th10">操作</th></tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < myList.length;i++){
                        var obj = myList[i];
                        var num = (pageIndex-1)*pageSize + i+1;
                        var user_name = obj.nick_name;     //团长昵称
                        var user_id = obj.user_id;
                        var good_name = obj.name;    //商品名称
                        var gid = obj.gid;
                        var open_num = obj.open_num;      //开团人数
                        var residue_num = obj.residue_num;  //剩余人数
                        var is_success = obj.is_success;

                        var id = obj.id;
                        
                        if(is_success==1){
                            is_success="开团成功";
                        }
                        else if(is_success==0){
                            is_success="拼团中"
                        }
                        else if(is_success==-1){
                            is_success="拼单失败"
                        }

                        var checked = (idList.indexOf(id) >= 0) ? "checked":"";//判断当前记录先前有没有被选中

                        html+='<tr>'
                        	+'<td><input type="checkbox" class="select-single my-icheckbox" value="'+id+'" '+checked+'></td>'
                            +'<td>'+num+'</td>'
                            +'<td><span class="limit-name">'+good_name+'</span></td>'
                            +'<td><a href="javascript:;" title="查看用户详情" class="user-detail" data-user_id="'+user_id+'">'+user_name+'</a></td>'
                            +'<td>'+open_num+'</td>'
                            +'<td>'+residue_num+'</td>'
                            +'<td>'+is_success+'</td>'
                            +'<td>'
                            +(getValue.isSee() == 1 ? '<a href="groupDetail?id='+id+'" class="btn btn-primary btn-xs delete-batch">详情</a>':'')
                            +(getValue.del() == 1 ?'<a href="javascript:;" class="btn btn-default btn-xs delete-batch" data-id="'+id+'">删除</a>':'')
                            +'</td>'
                            +'</tr>';
                    }

                    if(myList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    }else{
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList,".inner-section #list-table .select-all",".inner-section #list-table .select-single");
                        
                        //删除订单
                        $(".delete-order").click(deleteOrder);  
                    }
                }else{
                    responseTip(result.errorCode,result.errorInfo,1500);
                }
            },
            error:errorResponse
        });
    }

    /**
     * 删除
     * @param id
     */
    function deleteOrder(){
        var id = $(this).attr('data-id');
        myConfirmModal("确定删除吗？",function(){
            $.ajax(
                {
                    type:"post",
                    url:"deleteGroup",
                    data:{"ids":id},
                    dataType:"json",
                    beforeSend:function(xhr){
                        //显示“加载中。。。”
                        $("#loading").modal('show');
                    },
                    complete:function(){
                        //隐藏“加载中。。。”
                        $("#loading").modal('hide');
                    },
                    success:function(json,statusText){
                        if(json.errorCode == 0){
                            if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true,currentPage,pageSize);
                        }else{
                            responseTip(json.errorCode,json.errorInfo,1500);
                        }
                    },
                    error:errorResponse
                }
            );
        });

    }
    
    init();
});